package com.qa.mapper;

import com.qa.annotation.AutoFill;
import com.qa.entity.Note;
import com.qa.enumeration.OperationType;
import com.qa.vo.NoteSelfVO;
import com.qa.vo.NoteVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface NoteMapper {

    /**
     * 根据题目查询(公开、存在的)笔记,带有创建者昵称,自己的笔记不公开自己也能看见
     *
     * @param questionId 题目id
     * @param userId     当前用户id
     * @return List NoteVO
     */
    @Select("SELECT n.id,question_id,content,n.status,creator,time,open, u.nickname\n" +
            "    FROM note as n,user as u\n" +
            "    WHERE question_id = #{questionId} AND n.status = 1 AND u.id = n.creator AND IF(n.creator = #{userId},TRUE,n.open = 1)")
    List<NoteVO> getNotesByQuestionId(@Param("questionId") Long questionId, @Param("userId") Long userId);

    /**
     * 添加笔记
     *
     * @param note 笔记
     * @return boolean
     */
    @Insert("INSERT INTO note (id, question_id, content, status, creator, time, open) " +
            "VALUES(#{id}, #{questionId}, #{content}, #{status}, #{creator}, #{time}, #{open})")
    @AutoFill(value = OperationType.INSERT)
    boolean addNote(Note note);

    /**
     * 根据用户id查询所有存在的笔记
     *
     * @param userId 用户id
     * @return List NoteSelfVO
     */
    @Select("select * from note where creator = #{userId} and status = 1")
    List<NoteSelfVO> getNotesByUserId(Long userId);

    /**
     * 条件查询所有笔记
     *
     * @param note 笔记查询条件
     * @return List NoteSelfVO
     */
    List<Note> getNotesByCondition(Note note);

    /**
     * 更新笔记
     *
     * @param note 笔记
     * @return boolean
     */
    @AutoFill(value = OperationType.UPDATE)
    boolean updateNote(Note note);

    /**
     * 批量删除笔记
     *
     * @param ids 笔记id
     * @return boolean
     */
    int deleteNotes(@Param("ids") List<Long> ids);
}
